package com.xiaomi.web.handlers;

import com.github.pagehelper.PageInfo;
import com.xiaomi.domain.CategoryEntity;
import com.xiaomi.domain.UserEntity;
import com.xiaomi.service.CateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CateHandler {
    @Autowired
    private CateService cateService;
    @RequestMapping(name = "添加分类数据",value = "/insertCate")
    public String insertCate(HttpServletRequest request){
        //获取请求数据
        CategoryEntity categoryEntity=null;
        String cname=request.getParameter("cname");
        String state=request.getParameter("state");
        String orderNumber=request.getParameter("order_number");
        String description=request.getParameter("description");
        String createTime=request.getParameter("create_time");
        String sid=null;


        //封装到实体类中
        try{
            categoryEntity=new CategoryEntity(sid,cname,state,orderNumber,description,createTime);
        } catch (Exception e){
            throw new  RuntimeException("出错了",e);
        }
        System.out.println(categoryEntity);
        cateService.addCate(categoryEntity);
        return "admin/category_list";

    }
    @RequestMapping(name = "获取所有商品分类数据，封装转发",value = "/findPageCates")
    public String findAll(@RequestParam(defaultValue = "1")Integer pageNum, HttpServletRequest request){
        if(pageNum<=0){
            pageNum=1;
        }
        PageInfo<CategoryEntity> cates=null;
       try {
       cates=cateService.findAllCates(pageNum,5);
       }catch (Exception e){
           System.out.println("出错了，cateSerice");
       }
        HttpSession session=request.getSession();

        session.setAttribute("cates",cates);
        return "admin/category_list";

    }
    @RequestMapping(name = "根据id查找cate",value = "/findCateById")
    public String findCateById(String cid,HttpServletRequest request){
        CategoryEntity categoryEntity=cateService.findById(cid);
        HttpSession session=request.getSession();
        session.setAttribute("cateById",categoryEntity);

        return "admin/category_update";
    }
    @RequestMapping(name = "封装更新cate",value = "/cateAlter")
    public void cateAlter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CategoryEntity categoryEntity=null;
        String cname=request.getParameter("cname");
        String state=request.getParameter("state");
        String orderNumber=request.getParameter("order_number");
        String description=request.getParameter("description");
        String createTime=request.getParameter("create_time");
        String sid=request.getParameter("cid");
        //封装到实体类中
        try{
            categoryEntity=new CategoryEntity(sid,cname,state,orderNumber,description,createTime);
            cateService.cateAlter(categoryEntity);
        } catch (Exception e){
            throw new  RuntimeException("出错了",e);
        }
        request.getRequestDispatcher("findPageCates").forward(request,response);
    }
    @RequestMapping("/deleteCatesById")
    public void deleteCatesById(String[] ids,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        cateService.delets(ids);

        request.getRequestDispatcher("findPageCates").forward(request,response);
    }
}
